package com.zh.baselibrary.netutils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.JsonUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.ShareManager;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2018/6/27.
 */

public class HRetrofitNetHelper {

    private static Retrofit mRetrofit;
    //0开发 1 测试 2 升级测试内网 3测试外网 4升级测试外网 5生产6陪标
    public static final int TYPE = 5;
    public static  boolean isJdb = true;  //不用改
    private static List<String> urlList = Arrays.asList("172.16.6.31","172.16.6.35","172.16.6.137","112.111.13.209","112.111.13.209","zhuangyuanpai.com.cn","112.111.13.209");
    private static List<String> jdbList = Arrays.asList("8050","8050","8050","4158","4183","8050","4205");
    private static  List<String> ircList = Arrays.asList("8060","8060","8060","4159","4182","8060","8060");
    private static List<String> downLoadList = Arrays.asList(":80",":80",":80",":4188",":4187","",":80");
    public static Retrofit retrofit() {

        if (mRetrofit == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.writeTimeout(10, TimeUnit.SECONDS);
            builder.readTimeout(10, TimeUnit.SECONDS);
            builder.connectTimeout(10, TimeUnit.SECONDS);
            builder.addInterceptor(new LoggingInterceptor());

            OkHttpClient okHttpClient = builder.build();
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(getHttpUrl())
                    .addConverterFactory(GsonConverterFactory.create(new Gson()))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }

        return mRetrofit;
    }

    public static String getHttpUrl(){
        if(isJdb)
        return  "http://"+urlList.get(TYPE)+":"+jdbList.get(TYPE);
        return  "http://"+urlList.get(TYPE)+":"+ircList.get(TYPE);
    }
    public static String getWebSocketUrl(){
        if(isJdb)
        return  "ws://"+urlList.get(TYPE)+":"+jdbList.get(TYPE)+"/jdb/websocket/";
        return  "ws://"+urlList.get(TYPE)+":"+ircList.get(TYPE)+"/irc/websocket/";
    }
    public static  String getDownloadUrl(){
        return  "http://"+urlList.get(TYPE)+downLoadList.get(TYPE);
    }


    /**
     * Log拦截
     */
    static class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            //发送时截取数据

            Request original = chain.request();
            String token = BaseConstant.DEVICE_TOKEN;
            if(!TextUtils.isEmpty(BaseConstant.DEVICE_TOKEN2))
                token = BaseConstant.DEVICE_TOKEN2;
            Request request = original.newBuilder()
                    .header("token", token)
                    .method(original.method(), original.body())
                    .build();

            long t1 = System.nanoTime();//请求发起的时间
            LogUtils.e(String.format("发送请求:%s",request.url()));
            //接收时截取数据
            Response response = chain.proceed(request);
            long t2 = System.nanoTime();//收到响应的时间
            ResponseBody responseBody = response.peekBody(1024 * 1024);
            LogUtils.d(String.format(Locale.CHINA, "接收响应:%s%n请求时间:%.1fms%n报文信息:%s返回JSON:%s",
                    response.request().url(),
                    (t2 - t1) / 1e6d,
                    response.headers(),
                    responseBody.string()));
            return response;
        }
    }
}
